
#ifndef GLOBAL_PARA_HPP
#define GLOBAL_PARA_HPP

#define USR_BYTE0(USR_TEMP)     (USR_TEMP & 0xFF)
#define USR_BYTE1(USR_TEMP)     ((USR_TEMP >> 8) & 0xFF)
#define USR_BYTE2(USR_TEMP)     ((USR_TEMP >> 16) & 0xFF)
#define USR_BYTE3(USR_TEMP)     ((USR_TEMP >> 24) & 0xFF)

#define CIRCLE_BUF_SIZE         4096     //! 环形缓冲区大小

#define toRAD                                 0.01745329251994329577      //! factor of transforming degree to rad
#define toDEG                                57.29577951308232088      //! factor of transforming rad to degree
#define PI                                        3.1415926535897932384626
#define LATLNG_TO_M             0.01113195
#define DISTANCE                        8.0
#define KP                                        0.24

typedef struct vector2
{
    int16_t x;
    int16_t y;
}vector2_t;


typedef struct remote_control
{
    uint16_t UAV_acc;
    uint16_t UAV_roll;
    uint16_t UAV_pitch;
    uint16_t UAV_yaw;
     uint16_t UAV_land;
}remote_control_t;
extern remote_control_t remote_cmd;//遥控器指令

typedef struct waypoint_flight_control
{
    uint16_t point_number;   //航点总数
    uint16_t current_index;  //当前航点的序号
    uint16_t next_index;    //下一航点序号
    uint8_t point_property;   //航点高度模式
    int16_t point_i_lon;     //航点经度整数
    int32_t point_f_lon;    //航点经度小数
    int16_t point_i_lat;     //航点纬度整数
    int32_t point_f_lat;      //航点纬度小数
    int32_t point_altitude;    //航点高度
    int16_t point_velocity;   //航点水平速度
    int16_t point_v_circle;    //航点盘旋速度
    int16_t point_climb_rate;   //航点垂直速度
    uint8_t point_mode;    //航点模式
    uint16_t point_duration;    //持续时间（圈）
    uint16_t point_radius;     //盘旋半径
    int16_t point_heading;   //航点指向
    uint16_t point_distance;    //到点距离（cm）
    uint8_t point_take_cmd;   //航点任务模式、执行模式、执行次数
    uint16_t point_task_interval;    //航点任务间隔（秒）
    uint8_t point_task_cmd;   //航点任务命令
    uint32_t point_waypointID;   //航线ID
}waypoint_flight_control_t;
extern waypoint_flight_control_t flight_control_waypoint;//用来储存飞控回传的航点信息

typedef struct waypoint_groundbase
{
    uint16_t point_number;
    uint16_t current_index;
    uint16_t next_index;
    uint8_t point_mode; 
    int32_t point_lat;
    int32_t point_lng;
    int32_t point_alt;
    uint16_t point_duration;
}waypoint_groundbase_t;
extern waypoint_groundbase_t groundbase_waypoint;//用来储存地面站需要的航点信息

typedef struct vector3
{
    int16_t x;
    int16_t y;
    int16_t z;
}vector3_t;

typedef struct vector3f
{
   float x;
   float y;
   float z;
}vector3f_t;

typedef struct circular_buffer
{
    uint8_t  buf[CIRCLE_BUF_SIZE];
    uint16_t head;
    uint16_t tail;
}circleBuf_t;
extern  circleBuf_t groundbase_ringbuf;            //地面站环形缓冲区
extern  circleBuf_t flightcontrol_ringbuf;    //飞控环形缓冲区
extern  circleBuf_t camera_ringbuf;           //相机环形缓冲区
extern circleBuf_t SBUS_ringbuf;              //SBU遥控器环形缓冲区
extern float pos_error_kp;

extern bool if_up_and_down_waypoint_falg;
typedef struct waypoinit
{
    uint8_t point_mode;
    uint16_t point_duration;
    int32_t lat;
    int32_t lng;
    int32_t alt;
}waypoinit_t;

typedef struct usr_flag
{
    bool thread_quit;       //! 线程退出
    bool control_flag;       //控制指令是否发送成功
    bool flight_point_flag;   //航点指令是否发送成功
    bool self_check_flag;   //自检标志位
    bool who_control_flight;  //控制优先级选择标志   遥控器 or 地面站？
    bool final_waypoint_flag;
    // bool SBUS_flag;
    bool query_1;
    bool query_2;
    bool query_3;
    bool query_4;
    bool query_7;
    bool query_8;

    uint8_t NX_wait_groundbase_controlcmd_ready_flag;

    uint8_t flight_ID;
    bool remote_lose_rc_link_flag;

    bool gcs_lose_heart_beat_flag;
    uint8_t NX_get_groundbase_heartbeat_cnt;

    bool onboard_takeoff_flag;
    bool onboard_land_flag;

    bool remote_control_flag;

}usr_flag_t;
extern usr_flag_t usr_flag;     


typedef struct groundbase_cmd
{
    uint8_t control_cmd; // 地面站命令

    int16_t roll_control;                 //飞机滚动
    int16_t pitch_control;             //飞机俯仰
    int16_t acc_control;                //飞机油门
    int16_t point_to_control;     //飞机指向
    int16_t pan_tilt_pitch;           //云台俯仰
    int16_t pan_tilt_point_to;    //云台指向
     float x_velocity;
    float y_velocity;
    float z_velocity;
    float yaw_rate;

    uint8_t query_cmd;      //查询指令
    uint16_t current_index; 

    uint16_t current_up_index; ///当前请求的上传航点的id号

    uint8_t target_pos_x1_point; // 图像上的位置  1
    uint8_t target_pos_y1_point;
    uint8_t target_pos_x2_point; // 图像上的位置  1
    uint8_t target_pos_y2_point;
    uint16_t equip_angle;   // 吊仓当前角度
 
    uint8_t angle_rate;
    int16_t gimbal_yaw;
    int16_t gimbal_pitch;
    int16_t gimbal_yaw_rate;
    int16_t gimbal_pitch_rate;

    int16_t target_x;    
    int16_t target_y;
}groundbase_cmd_t;
extern groundbase_cmd_t groundbase_cmd;  //地面站控制指令

typedef struct flight_control_to_NX_data
{
  uint8_t cmd_ret;    //命令执行结果
  uint8_t gps_num;    //卫星数
  uint8_t ahrss_state;     //定位状态

  int16_t i_lon;    //经度整数
  int32_t f_lon;    //经度小数

  int16_t i_lat;    //纬度整数
  int32_t f_lat;    //纬度小数

  int32_t alt;     //海拔高度

  int16_t vn;    //北向速度
  int16_t ve;    //东向速度
  int16_t vu;    //天向速度

  int16_t phi;   //飞机滚转角
  int16_t  the;   //飞机俯仰角
  uint16_t psi;   //飞机指向角

  int16_t gyr_x;   //陀螺仪X轴
  int16_t gyr_y;  
  int16_t gyr_z;  

  int16_t acc_x;   //加速度计X轴
  int16_t acc_y;
  int16_t acc_z;

  int16_t mag_x;  //磁罗盘X轴
  int16_t mag_y; 
  int16_t mag_z; 

  uint8_t home_lon_lat_valid;   //返航点标志位  0：无效   1：有效

  int16_t home_i_lon;    //返航点经度整数
  int32_t home_f_lon;   //返航点经度小数

  int16_t home_i_lat;    //返航点纬度整数
  int32_t home_f_lat;    //返航点纬度小数

  int32_t home_alt;    //返航点高度

  uint16_t battery_vpower;    //电池电压

  uint8_t firmware_subver;    //子版本号

  uint8_t firmware_ver;    //主版本号

  uint32_t fly_mileage;    //飞行总里程

  uint32_t fly_sorties;    //飞行总架次

  int32_t fly_time;    //飞行总时长

  uint8_t aircraft_power_motor_status;    //电池、电机状态

  uint8_t takeoff_landing_on_ground_flag;       //起飞、降落、在地标志

  uint8_t flight_mode;       //飞行模式

  uint8_t navi_sub_mode;     //导航子模式

  uint8_t breakdown_flag;     //故障标志

  uint8_t limit_lost_fail_flag;   //极限、失控标志

  uint8_t low_fail_flag;    //信号弱标志位

  uint16_t smart_battery_status;   //智能电池状态

}flight_control_to_NX_data_t;
extern flight_control_to_NX_data_t flight_control_to_NX_data ; //从飞控获取的数据


typedef struct drone_data_to_groundbase
{
    uint8_t selfcheck;    // UAV zi jian 

    uint8_t flight_mode; //飞行模式
    uint8_t flight_status;// 飞行状态
    uint8_t fix_status; // 定位状态
    uint8_t battery;   // 电池电量
    uint8_t cmd_feedback; //无人机反馈

    int16_t roll;
    int16_t pitch;
    int16_t yaw;


    vector3_t vel;

    waypoinit_t curr_pos;

    uint8_t   equip_status;  // 吊仓当前状态
    int16_t  gimbal_pitch; // 吊仓当前pitch
    int16_t gimibal_yaw;// 吊仓当前yaw
    
}drone_data_to_groundbase_t;
 extern drone_data_to_groundbase_t drone_data_to_groundbase;   //飞控数据写，地面站读  （向地面站发）


typedef struct time
{
    uint16_t year;
    uint8_t mon;
    uint8_t day;
    uint8_t hour;
    uint8_t min;
    uint8_t sec;
}user_time_t;
extern  user_time_t curr_time;

typedef struct   target_vel{

 float target_vel_x;
 float target_vel_y;

}target_vel_t;
extern target_vel_t target_velocity;



union int32_float
{
    int32_t data_int32;
    float data_float;
};

union uint64_double
{
    double data_double;
    uint64_t data_uint64;
};


 extern bool NX_get_start_up_waypoint_flag;   ////接收到地面站开始上传航点标志位
 extern bool NX_get_up_waypoint_mess_flag;    ///得到飞控单个上传航点信息标志位
 extern bool NX_get_up_waypoint_single_flag ;   ///接收到地面站单个上传航点指令标志位
 extern bool NX_get_up_waypoint_final_flag ;    ///最后一个航点标志位
 extern uint8_t NX_get_up_down_waypoint_cnt;

extern uint16_t _channels[16];


extern bool ground_cmd_flag ;




extern uint16_t TF_mini_data ;
extern uint16_t TF_strength;
extern int file;
extern int UDP_fd;

extern int flight_control_lose_massage_cnt;
extern bool flight_control_lose_massage_flag;


void usr_para_init(void);


uint16_t CalculateCrc16(volatile uint8_t *ptr,uint8_t len) ;    //CRC校验
uint8_t get_BCC(uint8_t* data,uint16_t length);                 //bcc校验

#endif